%function [c_matrCHOL] = CholeskyGenerator(matrixSize, fileName, fileNameTransposed, fileNameCHOL, fileNameWeight)
% function [c_matrCHOL] = CholeskyGenerator(matrixSize, fileName, fileNameCHOL, fileNameForSub, fileNameBackSub, fileNameWeight)
function [c_matrCHOL] = CholeskyGenerator(matrixSize, fileName, fileNameCHOL)
	fprintf(1, 'Generating an hermitian matrix\n');
	c_matrH = Hermitian(matrixSize);
	fprintf(1, 'Transform the hermitian matrix in a positive definite one\n');
	c_matrHP = c_matrH * c_matrH';
	fprintf(1, 'Writing on file the Hermitian Positive Definite Matrix\n');	
	fp = fopen(fileName, 'w');
	fprintf(fp, '%d ', matrixSize);
	for k=1:matrixSize
		for l=1:matrixSize
			fprintf(fp, '%f ',real(c_matrHP(l,k)));
			fprintf(fp, '%f ',imag(c_matrHP(l,k)));
		end
	end
	fclose(fp);

    stVector = single(zeros(matrixSize, 1));
    %generate a random Steering Vector
    fprintf(1, 'Generating the STEERING VECTOR\n');
    for index = 1:matrixSize
        stVector(index, 1) = 1 + (1i * 0);
    end
    
%	fprintf(1, 'Write the Positive Hermitian Matrix in transposed form on file\n');		
%	fp = fopen(fileNameTransposed, 'w');
%	fprintf(fp, '%d ', matrixSize)
%	for k=1:matrixSize
%		for l=1:matrixSize
%			fprintf(fp, '%f ',real(c_matrHP(k,l)))
%			fprintf(fp, '%f ',imag(c_matrHP(k,l)))
%		end
%%Ch	fclose(fp);

	fprintf(1, 'Execute the Cholesky Factorization on the matrix\n');	
	c_matrCHOL = single(chol(single((c_matrHP))));
	fprintf(1, 'Write Cholesky result on file\n');		
	fp = fopen(fileNameCHOL, 'w');
	fprintf(fp, '%d ', matrixSize);
	fprintf(fp, 'MATLAB: Results of the factorization\n');
	for k=1:matrixSize
		fprintf(fp, '[ ');
		for l=1:matrixSize
			fprintf(fp, '%f ',real(c_matrCHOL(l,k)));
			fprintf(fp, '%f ',imag(c_matrCHOL(l,k)));
		end
		fprintf(fp, ' ]\n');
	end
	fprintf(fp, '\n');
	fclose(fp);

%     forSubVector = linsolve(c_matrCHOL, stVector);
%     fprintf(1, 'Writing on file the Forward Substitution Vector\n');	
% 	fp = fopen(fileNameForSub, 'w');
% 	for k=1:matrixSize
%         fprintf(fp, '[ ');
%         fprintf(fp, '%6.3f ',real(forSubVector(k)));
% 		fprintf(fp, '%6.3fi ',imag(forSubVector(k)));
%         fprintf(fp, ' ]\n');
% 	end
% 	fclose(fp);
    
%     backSubVector = linsolve(c_matrCHOL', forSubVector);
%     fprintf(1, 'Writing on file the Backward Substitution Vector\n');	
% 	fp = fopen(fileNameBackSub, 'w');
% 	for k=1:matrixSize
%         fprintf(fp, '[ ');
%         fprintf(fp, '%6.3f ',real(backSubVector(k)));
% 		fprintf(fp, '%6.3fi ',imag(backSubVector(k)));
%         fprintf(fp, ' ]\n');
% 	end
% 	fclose(fp);
    
%     %for OCTAVE compatibility comment the following 2 lines
%     linopts.SYM = true; linopts.POSDEF = true;
%     weights = linsolve(c_matrHP, stVector, linopts);
%     fprintf(1, 'Writing on file the Weights Vector\n');	
% 	fp = fopen(fileNameWeight, 'w');
% 	for k=1:matrixSize
%         fprintf(fp, '[ ');
%         fprintf(fp, '%6.3f ',real(weights(k)));
% 		fprintf(fp, '%6.3fi ',imag(weights(k)));
%         fprintf(fp, ' ]\n');
% 	end
% 	fclose(fp);

    
end